package com.jzr.medical.controller.test;

import com.jzr.netty.common.protocol.GetDeviceListRequest;
import com.jzr.netty.common.protocol.GetDeviceListResponse;
import com.jzr.netty.common.protocol.RegisterRequest;
import com.jzr.netty.common.protocol.RegisterResponse;
import com.jzr.netty.common.service.AdminNettyClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RequestMapping("/test/netty")
@RestController
public class NettyController {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Value("${nettyServer.ip}")
    private String nettyServerIp;

    @Value("${nettyServer.port}")
    private int nettyServerPort;

    @GetMapping("/register")
    public RegisterResponse register() throws Throwable {
        RegisterRequest request = new RegisterRequest();
        request.setDeviceIp("127.0.0.2");
        request.setDeviceType(1);
        request.setDeviceNo("test");
        request.setAppVersion((float)1.1);
        request.setDeviceMac("MAX.100.1001.11");
        request.setNurseboardIp("127.0.0.3");
        request.setDeptCode("100222");
        request.setLastUpTime(new Date());
        try(AdminNettyClient client = new AdminNettyClient(nettyServerIp,nettyServerPort)) {
            RegisterResponse response = (RegisterResponse) client.send(request);
            if (response != null) {
                log.info("NettyController response.getIp(): " + response.getDeviceIp());
            } else {
                log.info("NettyController response is null");
            }
            return response;
        }
    }
    @GetMapping("/getDeviceList")
    public GetDeviceListResponse getDeviceList() throws Throwable {
        GetDeviceListRequest request = new GetDeviceListRequest();
        request.setDeviceType(null);
        try(AdminNettyClient client = new AdminNettyClient(nettyServerIp,nettyServerPort)) {
            GetDeviceListResponse response = (GetDeviceListResponse) client.send(request);
            if (response != null) {
                log.info("NettyController response.getIp(): " + response.getDeviceIp());
            } else {
                log.info("NettyController response is null");
            }
            return response;
        }
    }
}
